package com.atldy.redis.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @version 1.0
 * @Author: li-dong-yang
 * @CreateTime: 2021-09-16 17:10
 * @Description： swagger 配置1
 */
@Configuration
@Import(cn.hutool.extra.spring.SpringUtil.class)
@EnableSwagger2
public class SwaggerWebMvcConfig implements WebMvcConfigurer {


    @Autowired
    protected Environment env;

    /**
     * 静态资源放行
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //配置swagger静态资源放行
        registry.addResourceHandler("/**").
                addResourceLocations("classpath:/static/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }


    /**
     * 可以定义多个组
     */
    @Bean
    public Docket createDocket() {

            Docket docket = new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(ApiInfo())
                    .forCodeGeneration(true)
                    .directModelSubstitute(java.nio.ByteBuffer.class, String.class)
                    .select()
                    //这里指定Controller扫描包路径
                    .apis(RequestHandlerSelectors.basePackage("com.atldy.redis.controller"))
                    .paths(PathSelectors.any())
                    .build();
            return docket;
    }


    private ApiInfo ApiInfo() {
        String appName = env.getProperty("spring.application.name");
        if (appName == null) {
            appName = "Demo rest api";
        }

        return new ApiInfoBuilder()
                .title(appName)//大标题
                .description(appName + " RESTful API文档")//详细描述
                .version("1.0")//版本
                .termsOfServiceUrl("Hangzhou Zheng Jia Technology Co., Ltd.")
                .contact(new Contact("Hangzhou Zheng Jia Technology Co., Ltd.", "", "974213337@qq.com"))//作者
                .license("The Apache License, Version 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .build();
    }


}